/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.co.pas.business.data.audit;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import za.co.pas.business.data.*;

/**
 *
 * @author alabuschagne
 */
@MappedSuperclass
public class GenericAudit implements Serializable
{

    private static final long serialVersionUID = 1L;
    protected Long id;
    protected Company company;
    protected UserLoginDetails changedBy;
    protected Calendar changedAt;
    protected String tableName;
    protected String colName;
    protected Long longId;
    protected String stringId;
    protected String oldValue;
    protected String newValue;

    public GenericAudit()
    {
    }

    protected GenericAudit(Company company, UserLoginDetails changedBy, Calendar changedAt,
            String tableName, String colName, Long longId,
            String stringId, String oldValue, String newValue)
    {
        set(company, changedBy, changedAt, tableName, colName, longId, stringId, oldValue, newValue);
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof GenericAudit))
        {
            return false;
        }
        GenericAudit other = (GenericAudit) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder(changedBy.getUserName());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sb.append("|");
        sb.append(sdf.format(changedAt.getTime()));
        sb.append("|");
        sb.append(tableName);
        sb.append("|");
        sb.append(colName);
        if (longId != null)
        {
            sb.append("|");
            sb.append(Long.toString(longId));
        }
        if (stringId != null)
        {
            sb.append("|");
            sb.append(stringId);
        }
        sb.append("|");
        sb.append(oldValue);
        sb.append("|");
        sb.append(newValue);
        return sb.toString();
    }

    /**
     * @return the changedBy
     */
    @OneToOne
    public UserLoginDetails getChangedBy()
    {
        return changedBy;
    }

    /**
     * @param changedBy the changedBy to set
     */
    public void setChangedBy(UserLoginDetails changedBy)
    {
        this.changedBy = changedBy;
    }

    /**
     * @return the changeAt
     */
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    public Calendar getChangedAt()
    {
        return changedAt;
    }

    /**
     * @param changeAt the changeAt to set
     */
    public void setChangedAt(Calendar changeAt)
    {
        this.changedAt = changeAt;
    }

    /**
     * @return the tableName
     */
    public String getTableName()
    {
        return tableName;
    }

    /**
     * @param tableName the tableName to set
     */
    public void setTableName(String tableName)
    {
        this.tableName = tableName;
    }

    /**
     * @return the colName
     */
    public String getColName()
    {
        return colName;
    }

    /**
     * @param colName the colName to set
     */
    public void setColName(String colName)
    {
        this.colName = colName;
    }

    /**
     * @return the longId
     */
    public Long getLongId()
    {
        return longId;
    }

    /**
     * @param longId the longId to set
     */
    public void setLongId(Long longId)
    {
        this.longId = longId;
    }

    /**
     * @return the stringId
     */
    public String getStringId()
    {
        return stringId;
    }

    /**
     * @param stringId the stringId to set
     */
    public void setStringId(String stringId)
    {
        this.stringId = stringId;
    }

    /**
     * @return the oldValue
     */
    public String getOldValue()
    {
        return oldValue;
    }

    /**
     * @param oldValue the oldValue to set
     */
    public void setOldValue(String oldValue)
    {
        this.oldValue = oldValue;
    }

    /**
     * @return the newValue
     */
    public String getNewValue()
    {
        return newValue;
    }

    /**
     * @param newValue the newValue to set
     */
    public void setNewValue(String newValue)
    {
        this.newValue = newValue;
    }

    private void set(Company company, UserLoginDetails changedBy, Calendar changedAt,
            String tableName, String colName, Long longId,
            String stringId, String oldValue, String newValue)
    {
        this.company = company;
        this.changedBy = changedBy;
        this.changedAt = changedAt;
        this.tableName = tableName;
        this.colName = colName;
        this.longId = longId;
        this.stringId = stringId;
        this.oldValue = oldValue;
        this.newValue = newValue;
    }

    /**
     * @return the company
     */
    @OneToOne
    public Company getCompany()
    {
        return company;
    }

    /**
     * @param company the company to set
     */
    public void setCompany(Company company)
    {
        this.company = company;
    }
}
